package cn.memset.code.functionInterface;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ConsumerExample {
    public static void main(String[] args) {
        //testConsumer();
        //testBiConsumer();
        //testConsumerAndThen();
    }

    public static void testConsumer() {
        List<String> list = Arrays.asList("Guangdong", "Zhejiang", "Jiangsu");

        // 消费 list 中的每一个元素
        list.forEach(s -> System.out.println(s));
    }

    public static void testBiConsumer() {
        Map<String, String> cityMap = new HashMap<>();
        cityMap.put("Guangdong", "Guangzhou");
        cityMap.put("Zhejiang", "Hangzhou");
        cityMap.put("Jiangsu", "Nanjing");

        // 消费 map中的每一个(key, value)键值对
        cityMap.forEach((key, value) -> {
            System.out.println(String.format("%s 的省会是 %s", key, value));
        });
    }

    public static void testConsumerAndThen() {
        Consumer<String> printUpperCase = s -> System.out.println(s.toUpperCase());
        Consumer<String> printLowerCase = s -> System.out.println(s.toLowerCase());

        // 组合得到一个新的 Consumer ：先打印大写样式，再打印小写样式
        Consumer<String> prints = printUpperCase.andThen(printLowerCase);

        List<String> list = Arrays.asList("Guangdong", "Zhejiang", "Jiangsu");
        list.forEach(prints);
    }
}
